package com.power.handler;

import cn.hutool.json.JSONUtil;
import com.power.result.R;
import com.power.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author 海晨忆
 * @Des 权限拒绝的handler
 * @Date 2025/8/8 17:18
 */
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        R fail = R.FAIL(401, "权限不足");
        String jsonStr = JSONUtil.toJsonStr(fail);
        ResponseUtils.write(response, jsonStr);
    }
}
